#!/usr/bin/env python
# -*- coding:utf-8 -*- 

from flask_restful import Resource, reqparse
from flask import jsonify

from ...controller.orderController import SsoUserController
from ...utils import commons
from ...utils.response_code import RET
from ...utils.validate_operate import verify_user_role, verify_operator_object


class SsoUserResource(Resource):

    # get
    @classmethod
    def get(cls, orderID=None):
        if orderID:
            kwargs = {
                'orderID': orderID
            }

            res = SsoUserController.get(**kwargs)
            if res['code'] == RET.OK:
                return jsonify(code=res['code'], message=res['message'], data=res['data'])
            else:
                return jsonify(code=res['code'], message=res['message'], data=res['data'])

        parser = reqparse.RequestParser()
        parser.add_argument('orderID', location='args', required=False, help='orderID')
        parser.add_argument('userID', location='args', required=False, help='userID')
        parser.add_argument('thingID', location='form', required=False, help='thingID')
        parser.add_argument('isdelete', location='args', required=False, help='isdelete')

        kwargs = parser.parse_args()
        kwargs = commons.put_remove_none(**kwargs)

        res = SsoUserController.get(**kwargs)
        if res['code'] == RET.OK:
            return jsonify(code=res['code'], message=res['message'], data=res['data'], totalPage=res['totalPage'], totalCount=res['totalCount'])
        else:
            return jsonify(code=res['code'], message=res['message'], data=res['data']) 

    # delete

    @classmethod
    def delete(cls):
        # if orderID:
        #     kwargs = {
        #         'orderID': orderID
        #     }
        #
        # else:
        parser = reqparse.RequestParser()
        parser.add_argument('orderID', location='json', required=False, help='orderID')
        parser.add_argument('userID', location='json', required=False, help='userID')
        parser.add_argument('thingID', location='json', required=False, help='thingID')
        parser.add_argument('isdelete', location='json', required=False, help='isdelete')
            
            # Pass in the ID list for multiple deletions
        parser.add_argument('orderID', type=str, location='json', required=False, help='orderID')

        kwargs = parser.parse_args()
        kwargs = commons.put_remove_none(**kwargs)

        res = SsoUserController.delete(**kwargs)

        return jsonify(code=res['code'], message=res['message'], data=res['data'])

    # put
    @classmethod
    # @verify_operator_object([1,2],SsoUserController)status
    def put(cls):
        # if not orderID:
        #     return jsonify(code=RET.NODATA, message='primary key missed', error='primary key missed')

        parser = reqparse.RequestParser()
        parser.add_argument('orderID', location='json', required=False, help='orderID')
        parser.add_argument('userID', location='json', required=False, help='userID')
        parser.add_argument('thingID', location='json', required=False, help='thingID')
        parser.add_argument('isdelete', location='json', required=False, help='isdelete')
        
        kwargs = parser.parse_args()
        kwargs = commons.put_remove_none(**kwargs)

        res = SsoUserController.update(**kwargs)

        return jsonify(code=res['code'], message=res['message'], data=res['data'])

    # add
    @classmethod
    # @verify_user_role([3])
    def post(cls):
        '''
        SsoUserList: Pass in values in JSON format to batch add
        eg.[{k1:v1,k2:v2,...},...]
        '''
        parser = reqparse.RequestParser()
        parser.add_argument('SsoUserList', type=str, location='form', required=False, help='SsoUserList参数类型不正确或缺失')

        kwargs = parser.parse_args()
        kwargs = commons.put_remove_none(**kwargs)

        if kwargs.get('SsoUserList'):
            res = SsoUserController.add_list(**kwargs)

        else:
            parser.add_argument('orderID', location='json', required=False, help='orderID')
            parser.add_argument('userID', location='json', required=False, help='userID')
            parser.add_argument('thingID', location='json', required=False, help='thingID')
            parser.add_argument('isdelete', location='json', required=False, help='isdelete')
            
            kwargs = parser.parse_args()
            kwargs = commons.put_remove_none(**kwargs)

            res = SsoUserController.add(**kwargs)

        return jsonify(code=res['code'], message=res['message'], data=res['data'])
